using System;
using System.Collections.Generic;
using System.Text;
using PH.Common;
using System.Drawing;
using Hoshimi._Map_._Zone_;
using Hoshimi._Map_;

namespace Hoshimi
{
    public class FollowBot : Bot
    {
        protected NanoMoveable mNanoBot						= null;

        protected IFollowOwner mObjectToFollow				= null;

        protected Point mCurrentDestination					= Point.Empty;

		protected bool mUseFleeBehaviourWhenNoDestination	= true;
		protected bool mUseFleeBehaviour					= false;

		public bool UseFleeBehaviourWhenNoDestination
        {
            get { return this.mUseFleeBehaviourWhenNoDestination; }
            set 
			{
				this.mUseFleeBehaviourWhenNoDestination = value; 
				if (this.myStateMachine == null)
				{
					return;
				}
				if (value == false)
				{
					this.myStateMachine.SetGlobalState(null);
				}
			}
        }

		public bool UseFleeBehaviour
        {
            get { return this.mUseFleeBehaviour; }
            set 
			{
				this.mUseFleeBehaviour = value;

				if (this.myStateMachine == null)
				{
					return;
				}
				if (value == false)
				{
					this.myStateMachine.SetGlobalState(null);
				}
				else
				{
					this.myStateMachine.SetGlobalState(Bot_FleeState.Instance());
				} 
			}
        }


        public override NanoMoveable myNanobot
        {
            get { return this.mNanoBot; }
            set { this.mNanoBot = value; }
        }

        public IFollowOwner myObjectToFollow
        {
            get { return this.mObjectToFollow; }
            set { this.mObjectToFollow = value; }
        }

        public bool UpdateDestination()
        {
            if (!this.HasDestinationChanged())
            {
                return false;
            }

			if (this.mUseFleeBehaviourWhenNoDestination)
			{
				if (!this.HasDestination())
				{
					if (this.myStateMachine != null)
					{
						this.myStateMachine.SetGlobalState(Bot_FleeState.Instance());
					}
				}
				else if(!this.mUseFleeBehaviour)
				{
					if (this.myStateMachine != null)
					{
						this.myStateMachine.SetGlobalState(null);
					}
				}
			}

            this.mCurrentDestination = this.mObjectToFollow.GetDestination(this);
            return true;
        }

        public bool HasObjectToFollow()
        {
            return (this.mObjectToFollow != null);
        }

        public bool HasDestination()
        {
            return (!this.mCurrentDestination.IsEmpty);
        }

        public bool HasDestinationChanged()
        {
            if (!this.HasObjectToFollow())
            {
                if (!this.HasDestination())
                {
                    return false;
                }
                this.mCurrentDestination = Point.Empty;
                return true;
            }

            return (this.mObjectToFollow.GetDestination(this) != this.mCurrentDestination);
        }

        public void MoveToDestination()
        {
			if (this.mCurrentDestination.IsEmpty)
			{
				this.mCurrentDestination = myStrategyManager.OpponentNanoAILocation;
			}

            this.MoveTo(this.mCurrentDestination);
        }

        #region Ctor

        public FollowBot(NanoMoveable nanobot)
            : base()
        {
            this.mNanoBot = nanobot;

			if(this.BotType == BotTypeEnum.BaseExplorer)
			{
				this.mPathFinderConfig.mUseSameWeigth = true;
			}
        }

        #endregion
    }
}
